<?php

namespace api\modules\v1\services;

use common\models\File;
use common\models\CourseCount;
use common\utils\System;
use common\utils\Utils;

/**
 * Class Course
 * @package api\modules\v1\services
 */
class Course extends \common\models\Course
{
    /**
     * 返回列
     * @name: fields
     * @return array
     * @author: rickeryu <lhyfe1987@163.com>
     * @time: 2017/11/27 下午1:39
     */
    public function fields() {
        $fields =  parent::fields();
        $fields['image'] = function ($model){
            if( $model->file_id ){
                $file = $model->getFile()->asArray()->one();
                if ( ! $file  ){
                    return '';
                }else{
                    return $file['server'].$file['key'];
                }
            }else{
                return Utils::getHost();
            }
        };
        $fields['author'] = function ($model){
            return '于志远';
        };
        $fields['authorduty'] = function ($model){
            return '测试职务';
        };
        $fields['grade'] = function ($model){
            $count = $model->getCourseCount()->one();
            return !$count ? 3 : $count['grade'];
        };
        $fields['studynum'] = function ($model){
            $count = $model->getCourseCount()->one();
            return !$count ? 0 : $count['studynum'];
        };
        $fields['is_encrypt'] = function($model){
            return 111111;
        };
        $fields['is_select'] = function($model){
            return $model->getCourseSelectUser()->andWhere(['id'=>\Yii::$app->getUser()->getId()])->asArray()->one() ? 1 : 0;
        };

        if( \Yii::$app->controller->action->id == 'view' ){
            $fields['history'] = function($model){
                return CourseCatalog::getHistory($model->id);
            };
        }

        return $fields;
    }
}